import Entity.CreatEntityManager;
import Entity.Students;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "Cstudent",urlPatterns = {"/student"})
public class CreatStudents extends HttpServlet {
    private void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        EntityManager em = CreatEntityManager.GetEntityManager();
        EntityTransaction tx = em.getTransaction();
        Students Sa = new Students("201741412104", "黄敏仪", "软件工程", null);
        Students Sb = new Students("201741412107", "吴泽豪", "软件工程", null);
        try {
            tx.begin();
            em.persist(Sa);
            em.persist(Sb);
            tx.commit();
        } catch (Exception e) {

        } finally {
            em.close();
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("插入学生成功");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
